﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Premiere.DTO;
using System.Collections;
using Premiere.Window;
using System.Data;
using Premiere.Framework;

namespace PremiereMX
{
    public class OpeningStockLogic
    {
        OpeningStockDTO m_subject = new OpeningStockDTO();
        IMainForm m_mainForm;
        IUserControlOpeningStock m_form;
        EditMode m_editMode = EditMode.OK;
        IList m_partList;
        IList m_warehouseList, m_currencyList;
        IList m_fieldInfoList;
        bool m_isAutoNumber = false;

        public OpeningStockLogic(IMainForm mainForm, IUserControlOpeningStock form)
        {
            m_mainForm = mainForm;
            m_form = form;
        }
        public void Init()
        {
            //updateForm();
            loadList();
            m_isAutoNumber = ServiceRegistry.GetInstance().GetOpeningstockService().IsAutoNumber();
            m_form.SetIsAutoNumber(m_isAutoNumber);
            m_form.SetToReadOnly(true);
            SetButtonEditMode(EditMode.OK);
        }

        public void loadList()
        {
            //m_partList = ServiceRegistry.GetInstance().GetPartService().FindNamedPart();
            m_currencyList = ServiceRegistry.GetInstance().GetCurrencyService().LoadAll();
            m_warehouseList = ServiceRegistry.GetInstance().GetWarehouseService().FindWarehouseByWarehouseType(WarehouseType.Detail);
            m_fieldInfoList = ServiceRegistry.GetInstance().GetOpeningstockService().GetFieldInfos();
        }
        public IList CurrencyList
        {
            get { return m_currencyList; }
        }
        public IList WarehouseList
        {
            get { return m_warehouseList; }
        }
        private void updateForm()
        {
            m_form.Code = m_subject.Code;
            m_form.ComputerName = m_subject.ComputerName;
            m_form.LastUpdate = m_subject.LastUpdate;
            m_form.TotalAllAmount = m_subject.TotalAllAmount;
            m_form.TransactionCurrency = m_subject.TransactionCurrency;
            m_form.TransactionDate = m_subject.TransactionDate;
            m_form.UserUpdateBy = m_subject.UserName;
            m_form.Warehouse = m_subject.Warehouse;
            m_form.EventStatus = m_subject.EventStatus;
            m_form.Notes = m_subject.Notes;
            //m_form.ButtonPostBig.Text = old.EventStatus.Equals(EventStatus.Entry) ? "Post" : "Unpost";
            ArrayList items = (ArrayList)m_subject.EventItems;
            items.Sort(new ItemComparer());
            foreach (OpeningStockItemDTO item in items)
            {
                m_form.AddItem(item);
            }
        }
        private void SetButtonEditMode(EditMode editMode)
        {
            m_form.SetButtonEditMode(editMode);
            m_editMode = editMode;
        }
        public void Edit()
        {
            m_form.SetToReadOnly(false);
            m_form.SetButtonEditMode(EditMode.Insert);
        }
        private void updateSubject()
        {
            m_subject.Code = m_form.Code;
            m_subject.ComputerName = Environment.MachineName;
            m_subject.EmployeeMaster = null;
            m_subject.LastUpdate = DateTime.Now;
            m_subject.NoticeDate = DateTime.Now;
            m_subject.TotalAllAmount = m_form.TotalAllAmount;
            m_subject.TransactionCurrency = m_form.TransactionCurrency;
            m_subject.TransactionDate = m_form.TransactionDate;
            m_subject.UserName = m_mainForm.GetUser().Code;
            m_subject.Warehouse = m_form.Warehouse;
            m_subject.Vendor = null;
            m_subject.Notes = m_form.Notes;
            m_subject.EventItems.Clear();
            IList items = m_form.GetValidItems();
            foreach (OpeningStockItemDTO i in items)
                m_subject.AddItem(i);
        }
        internal void UpdateSubject(OpeningStockDTO curr)
        {
            m_subject = (OpeningStockDTO)ServiceRegistry.GetInstance().GetOpeningstockService().Get(curr.Id);
            m_form.SetToReadOnly(true);
            m_form.ClearForm();
            updateForm();
            SetButtonEditMode(EditMode.OK);
        }
        public void Save()
        {
            try
            {
                if (m_form.IsValidSubject())
                {
                    updateSubject();
                    m_subject.Id = ServiceRegistry.GetInstance().GetOpeningstockService().Save(m_subject);
                    m_subject.Code = ServiceRegistry.GetInstance().GetOpeningstockService().GetCodeById(m_subject.Id);
                    m_form.Code = m_subject.Code;
                    m_form.LastUpdate = m_subject.LastUpdate;
                    m_form.ComputerName = m_subject.ComputerName;
                    m_form.UserUpdateBy = m_subject.UserName;
                    m_form.ShowMessage("Transaction No. " + m_subject.Code + " is saved");
                    m_form.SetToReadOnly(true);
                    SetButtonEditMode(EditMode.OK);
                    //m_form.UpdateList(new object[] { m_subject });
                }
            }
            catch (Exception ex)
            {
                m_form.ShowMessage(ex.Message);
            }
        }
        internal void Delete()
        {
            if (m_subject.Id == 0) return;
            ServiceRegistry.GetInstance().GetOpeningstockService().Delete(m_subject);
            m_form.RemoveItem(m_subject.Id);
            m_form.ClearForm();
        }
        internal void New()
        {
            m_subject = new OpeningStockDTO();
            m_subject.TransactionDate = DateTime.Today;
            m_form.ClearForm();
            SetButtonEditMode(EditMode.Insert);
            m_form.SetToReadOnly(false);
            m_form.FocusToCode();
        }
        public void Refresh()
        {
        }
        internal void Print()
        {
            // DataSet ds = ServiceRegistry.GetInstance().GetOpeningstockService().GetPreviewReportData(m_subject.Id);
           // m_mainForm.Print(ds);
        }
        public IList FieldList
        {
            get { return m_fieldInfoList; }
        }
        public string Find(string filter)
        {
            m_form.ClearRecordList();
            ArrayList allresult = new ArrayList();
            foreach (FieldInfo f in this.FieldList)
            {
                if (f.ToString().Trim() == "Date") continue;
                if (f.ToString().Trim() == "Posted") continue;
                f.Value = filter;
                IList result = ServiceRegistry.GetInstance().GetOpeningstockService().FindByField(f);
                allresult.AddRange(result);
            }
            m_form.UpdateList(allresult);
            return "Found : " + allresult.Count.ToString() + " item(s)";
            //m_form.FoundRecord = "Found " + allresult.Count.ToString() + " item(s)";
        }
        private class ItemComparer : IComparer
        {

            #region IComparer Members

            public int Compare(object x, object y)
            {
                OpeningStockItemDTO xo = (OpeningStockItemDTO)x;
                OpeningStockItemDTO yo = (OpeningStockItemDTO)y;
                return new CaseInsensitiveComparer().Compare(xo.Id,yo.Id);
            }

            #endregion
        }
    }
}
